package cn.ac.ict.fpevaluation.main;

import cn.ac.ict.fpevaluation.bean.*;
import cn.ac.ict.fpevaluation.cluster.ap.APHelper;
import cn.ac.ict.fpevaluation.cluster.ap.WifiDistance;
import cn.ac.ict.fpevaluation.constant.Constant;
import cn.ac.ict.fpevaluation.evaluation.WifiEvaluation;
import cn.ac.ict.fpevaluation.evaluation.WifiEvaluationByDeltaHelper;
import cn.ac.ict.fpevaluation.evaluation.WifiEvaluationHelper;
import cn.ac.ict.fpevaluation.fusion.Fusion;
import cn.ac.ict.fpevaluation.fusion.FusionWifi;
import cn.ac.ict.fpevaluation.utils.DataOperationToDiskUtils;

import java.io.*;
import java.util.*;

public class Main {


    /**
     * 从轨迹融合到写入融合结果到硬盘
     */
    public static void fusionTest() {
        List<FusionBean> res = Fusion.fusion();
        DataOperationToDiskUtils.writeFusionResultToDisk(res);
    }

    public static void main(String[] args) {

        //地磁聚类
        //APHelper.launch();
        //WiFi聚类

        //wifiCluster();
        findIt();
//        parseConclustion();
    }

    private static void wifiCluster() {
        List<List<AtomTrajectoryBean>> wifiCluster = APHelper.wifiCluster();

        DataOperationToDiskUtils.writeClusterResultToDisk(wifiCluster, Constant.CLUSTER_RESULT);

    }

    private static void findIt() {
        FusionWifi fusionWifi = new FusionWifi();

        List<AtomTrajectoryBean> testData = DataOperationToDiskUtils.getOriginalData();

        for (AtomTrajectoryBean atomTrajectoryBean : testData) {
            List<List<AtomTrajectoryBean>> wifiCluster = DataOperationToDiskUtils.getClusters();
            // System.out.println("========================TEST=========================");
            //for (List<AtomTrajectoryBean> atomTrajectoryBeans : wifiCluster) {
            AtomTrajectoryBean fusion = fusionWifi.fusion(wifiCluster.get(0));
            WifiEvaluationByDeltaHelper.launch(fusion, atomTrajectoryBean);
            //}
        }

    }


    private static void parseConclustion() {
        String[] paths = new String[]{"2017-03-07", "2017-03-09", "2017-03-10"};
        Map<String, List<Double>> res = new HashMap<>();
        for (String path : paths) {
            getConclustion(path, res);
        }

        List<AtomTrajectoryBean> trajData = DataOperationToDiskUtils.getTrajData("/Users/feng/Desktop/GraduationThesis/Workspace/data/Conclusion/ssid");
        if (trajData != null && trajData.size() > 0) {
            Map<String, String> ssid = getSSID(trajData.get(0));
            res.keySet().forEach(item -> {
                if (res.get(item).size() > 1) {


                    System.out.print("Mac is: " + item + " and SSID is: " + ssid.get(item) + ", Distance is: ");
                    res.get(item).forEach(distance -> {
                        System.out.print(distance + ",");
                    });
                    System.out.println();

                }
            });
        }


    }


    private static Map<String, String> getSSID(AtomTrajectoryBean traj) {
        Map<String, String> ssid = new HashMap<>();
        List<List<APBean>> aps = traj.getAps();
        for (List<APBean> ap : aps) {
            for (APBean apBean : ap) {
                if (!ssid.containsKey(apBean.getMAC())) {
                    ssid.put(apBean.getMAC() + ",", apBean.getSSID());
                }
            }
        }

        return ssid;
    }

    private static void getConclustion(String fileName, Map<String, List<Double>> res) {
        String path = "/Users/feng/Desktop/GraduationThesis/Workspace/data/Conclusion";
        File file = new File(path + File.separator + fileName + ".txt");
        BufferedReader is = null;
        try {
            is = new BufferedReader(new FileReader(file));
            String data;
            while ((data = is.readLine()) != null) {
                String[] split = data.split(" ");
                if (split != null && split.length == 6) {
                    String mac = split[2];
                    double distance = Double.valueOf(split[5]);
                    if (!res.containsKey(mac)) {
                        res.put(mac, new ArrayList<>());
                    }
                    res.get(mac).add(distance);
                }
            }
            is.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

    }


}
